﻿#include "Creature.h"
#include "GameUtil.h"

using namespace cocos2d;

Creature::~Creature(void)
{
}

bool Creature::init(){  
    if (!GameObj::init()) {  
        return false;  
    }  
	
	// type
	this->setObjType(OBJ_TYPE_ENEMY);
	d_creatureState = CREATURE_STATE_NORMAL;

	// bullet
	this->setBulletType(BULLET_TYPE_1);
	this->setBulletSpeed(100.f);
	this->setBulletDamage(10.0f);
	this->setShootInterval(1.0f);
	
	// hp
	d_hpBar = HpBar::create(100.0f, 10.0f, 100.0f);
	this->setHasHpBar(true);
	this->setMaxHP(100.0f);
	this->setHP(100.0f);
	
	// money
	d_money = 10;

	// shoot and hit

	d_bulletManager = GameUtil::instance()->getBulletManager();
	d_actionTintTag = kCCActionTagInvalid;

    return true;  
} 

void Creature::afterInit()
{
	// param
	d_maxHP = d_setting->getHP(d_maxHP, this);
	d_hp = d_maxHP;
	d_shootInterval = d_setting->getShootInterval(d_shootInterval, this);
	d_bulletDamage = d_setting->getDamage(d_bulletDamage, this);
	d_bulletSpeed = d_setting->getBulletSpeed(d_bulletSpeed, this);
	d_money = d_setting->getMoney(d_money, this);
	CCSize spriteSize = d_creatureSprite->getContentSize();
	// hp bar
	float hpBarHeight = 0.0f;
	if(d_hasHpBar) {
		hpBarHeight = 10.0f;
		d_hpBar = HpBar::create(spriteSize.width, hpBarHeight, d_maxHP);
		this->addChild(d_hpBar);
		d_hpBar->setPosition(ccp(spriteSize.width/2, spriteSize.height + hpBarHeight/2));
		d_hpBar->setHp(d_hp);
	}
	
	// sprite
	this->addChild(d_creatureSprite);
	d_creatureSprite->setPosition(ccp(spriteSize.width/2, spriteSize.height/2));

	// creature size
	this->setContentSize(CCSize(spriteSize.width, spriteSize.height + hpBarHeight));
}

CCRect Creature::getHitRect()
{
	return GameUtil::getRect(0.9f, d_creatureSprite);
}

void  Creature::startShoot(bool firstShoot)
{
	d_creatureState = CREATURE_STATE_SHOOT;
	if(firstShoot) {
		shoot(.0f);
	}
	this->schedule(schedule_selector(Creature::shoot), d_shootInterval);
}

void Creature::stopShoot()
{
	if(d_creatureState == CREATURE_STATE_SHOOT) {
		d_creatureState = CREATURE_STATE_NORMAL;
	}
	this->unschedule(schedule_selector(Creature::shoot));
}

float Creature::onHit(float damage)
{
	return damage; // enemy need rewrite this function to calulate new damage
}

bool Creature::hit(float damage)
{
	d_hp -= onHit(damage);
	
	if(d_hp < 1) {
		// die
		this->d_creatureState = CREATURE_STATE_DYING;
		stopShoot();

		this->stopAllActions();
		d_creatureSprite->stopAllActions();
		d_hp = 0.0f;
		d_hpBar->setVisible(false);

		d_creatureSprite->setColor(ccc3(255,255,255));
		d_creatureSprite->runAction(
			CCSequence::create(
				getDieAction(),
				CCCallFuncN::create(this, callfuncN_selector(Creature::dead)), 
				NULL));

		return true;
	}

	d_hpBar->setHp(d_hp);

	// hit animation
    d_creatureSprite->setColor(ccc3(255,0,0));
	if(d_actionTintTag != kCCActionTagInvalid) {
		d_creatureSprite->stopActionByTag(d_actionTintTag);
	}
	CCAction* action = CCTintTo::create(0.5f, 255, 255, 255);
	d_actionTintTag = action->getTag();
	d_creatureSprite->runAction(action);

	return false;
}

void Creature::dead(CCNode* sender)
{
	this->d_creatureState = CREATURE_STATE_DEAD;
	this->setVisible(false);
	this->stopAllActions();

	onDead();
}

CCFiniteTimeAction* Creature::getDieAction()
{
	return CCFadeOut::create(1.0f);
}